-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add as_string() to the Cmdline crate #169
base: main
Are you sure you want to change the base?
Conversation
20cf9f9
to
431d52c
Compare
I remember having this discussion in the past, and I believe it was the case that |
Hello, thanks for the feedback! /// Convert a Cmdline to String
///
/// # Examples
///
/// ```rust
/// # use linux_loader::cmdline::*;
/// let mut cl = Cmdline::new(20).unwrap();
/// cl.insert_str("foo").unwrap();
/// cl.insert_init_args("bar").unwrap();
/// assert_eq!(String::try_from(&cl).unwrap(), "foo -- bar");
/// ```
impl TryFrom<&Cmdline> for String {
type Error = Error;
fn try_from(value: &Cmdline) -> result::Result<Self, Self::Error> {
if value.boot_args.is_empty() && value.init_args.is_empty() {
Ok("".to_string())
} else if value.boot_args.is_empty() {
Err(Error::NoBootArgsInserted)
} else if value.init_args.is_empty() {
Ok(value.boot_args.to_string())
} else {
Ok(format!(
"{}{}{}",
value.boot_args, INIT_ARGS_SEPARATOR, value.init_args
)
.to_string())
}
}
} And maybe this test: #[test]
fn test_string_from_cmdline() {
let mut cl = Cmdline::new(CMDLINE_MAX_SIZE).unwrap();
assert_eq!(String::try_from(&cl).unwrap(), "");
cl.insert_init_args("bar").unwrap();
// This must fail as it is not allowed to have only init args
let err = String::try_from(&cl).unwrap_err();
assert_eq!(err, Error::NoBootArgsInserted);
// However, inserting only bootargs is permitted
cl = Cmdline::new(CMDLINE_MAX_SIZE).unwrap();
cl.insert_str("foo").unwrap();
assert_eq!(String::try_from(&cl).unwrap(), "foo");
// Test also the concatenation of arguments
cl.insert_init_args("bar").unwrap();
assert_eq!(String::try_from(&cl).unwrap(), "foo -- bar");
}
} |
In some cases, having a String representation of the Linux command line can be useful. This is the case of vmm-reference which otherwise would require a conversion dance between string types. Signed-off-by: Alvise Rigo <[email protected]>
431d52c
to
76aefef
Compare
if value.boot_args.is_empty() && value.init_args.is_empty() { | ||
Ok("".to_string()) | ||
} else if value.boot_args.is_empty() { | ||
Err(Error::NoBootArgsInserted) | ||
} else if value.init_args.is_empty() { | ||
Ok(value.boot_args.to_string()) | ||
} else { | ||
Ok(format!( | ||
"{}{}{}", | ||
value.boot_args, INIT_ARGS_SEPARATOR, value.init_args | ||
) | ||
.to_string()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code ends up being almost the same as the as_cstring
function. Is there a way to not duplicate the code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. What about this:
impl TryFrom<&Cmdline> for CString {
type Error = Error;
fn try_from(value: &Cmdline) -> result::Result<Self, Self::Error> {
let as_string = <String as TryFrom<&Cmdline>>::try_from(value)?;
Ok(CString::new(as_string).map_err(|_| Error::NullTerminator)?)
}
}
In some cases, having a String representation of the Linux command line can be useful. This is the case of vmm-reference which otherwise would require a conversion dance between string types.
Summary of the PR
These changes are needed by vmm-reference which will be updated in another pull to use the a more recent version of this create. It turned out that without a way to directly cover to
String
, a dance of conversions was needed.Requirements
Before submitting your PR, please make sure you addressed the following
requirements:
git commit -s
), and the commitmessage has max 60 characters for the summary and max 75 characters for each
description line.
test.
Release" section of CHANGELOG.md (if no such section exists, please create one).
unsafe
code is properly documented.